package com.kifas.service.dataaccess;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kifas.entity.CustomerEntity;

@Repository
public class CustomerDao {

	@Autowired
	private SessionFactory sessionFactory;

	private static final String ALL_CUSTOMERS = "FROM CustomerEntity c left join fetch c.sales";
	private static final String CUSTOMER_BY_ID = ALL_CUSTOMERS
			+ " where idCustomer = :idCustomer";

	@SuppressWarnings("unchecked")
	public List<CustomerEntity> getCustomers() {
		return sessionFactory.getCurrentSession().createQuery(ALL_CUSTOMERS)
				.list();
	}

	public CustomerEntity getCustomerById(Long id) {
		return (CustomerEntity) sessionFactory.getCurrentSession()
				.createQuery(CUSTOMER_BY_ID).setLong("idCustomer", id)
				.uniqueResult();
	}

	public void insert(CustomerEntity customer) throws Exception {
		sessionFactory.getCurrentSession().save(customer);
	}
	
	public void update(CustomerEntity customer) throws Exception {
		sessionFactory.getCurrentSession().update(customer);
	}

}
